using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System.Diagnostics;
using System.ComponentModel;

namespace The_LarX
{
    public class ActorPlayer : Component, IHasActor
    {
        /// <summary>
        /// The Actor that this class is managing
        /// </summary>
        public Actor Actor
        {
            get { return actor; }
            set { actor = value; }
        }
        protected Actor actor;

        /// <summary>
        /// The RenderType Used
        /// </summary>
        public Renderer Renderer { get; set; }

        public ActorPlayer(Actor Actor, Renderer Renderer, GameScreen Screen)
            : base(Screen)
        {            
            this.Actor = Actor;
            this.Renderer = Renderer;
            this.Actor.ComponentParent = this;

            this.Actor.RemoveActorFromRenderer = Renderer.RemoveActor;
            this.Actor.AddActorToRenderer = Renderer.SubmitActor;
            this.Actor.OnDisable += new EventHandler(Actor_OnDisable);
            this.Renderer.SubmitActor(Actor);
        }

        void Actor_OnDisable(object sender, EventArgs e)
        {
            this.DisableComponent();
        }
        public ActorPlayer()
            : base()
        {            
        }

        public override void DisableComponent()
        {
            this.Actor.Disable();

            base.DisableComponent();
        }

        public override void Update()
        {
            Actor.Update();

            base.Update();
        }

        public override void Draw()
        {
            Actor.Draw();

            base.Draw();
        }

        public override string ToString()
        {
            return Actor.Name + "Player";
        }

        public override void SerializeData()
        {
            base.SerializeData();

            Actor.SerializeData(this.Data);
            // Serializa o Tipo do Actor
            Data.AddData("ActorPlayer.Actor", this.Actor.GetType().ToString());
            Data.AddData("Renderer.Name", this.Renderer.Name);
        }
        public override void DeserializeData()
        {
            base.DeserializeData();

            Type ActorType = Type.GetType(Data.GetData<string>("ActorPlayer.Actor"));

            Actor Object = (Actor)System.Activator.CreateInstance(ActorType);
           
            this.Actor = Object;

            Actor.DeserializeData(Data);

            string rendererName = Data.GetData<string>("Renderer.Name");
            if (!string.IsNullOrEmpty(rendererName))
            {
                this.Renderer = EngineSystem.Renderers[rendererName];
                Renderer.SubmitActor(Actor);
            }
            else
            {
                if (EngineSystem.Renderers.Count > 0)
                {
                    this.Renderer = EngineSystem.Renderers.Values.First();
                    Renderer.SubmitActor(Actor);
                }
            }
            this.Actor.RemoveActorFromRenderer = Renderer.RemoveActor;
            this.Actor.AddActorToRenderer = Renderer.SubmitActor;
            this.Actor.OnDisable +=new EventHandler(Actor_OnDisable);

            this.Actor.ComponentParent = this;            
        }

        public override void SetToInitialState()
        {
            base.SetToInitialState();

            Actor.SetToInitialState();
        }

        public override ICloneable Clone()
        {
            Actor actor = (Actor)this.Actor.Clone();
            ActorPlayer player = new ActorPlayer(actor, Renderer, this.Parent);
            return player;
        }
    }
}